"""

Template for Characters

Copy this module up one level and name it as you like, then
use it as a template to create your own Character class.

To make new logins default to creating characters
of your new type, change settings.BASE_CHARACTER_TYPECLASS to point to
your new class, e.g.

settings.BASE_CHARACTER_TYPECLASS = "game.gamesrc.objects.mychar.MyChar"

Note that objects already created in the database will not notice
this change, you have to convert them manually e.g. with the
@typeclass command.

"""
from ev import Character, utils
from game.gamesrc.objects.object import MyObject

class MyCharacter(MyObject, Character):
    """
    This is the main character class, mobs inherit from here.
    The Character is like any normal Object (see example/object.py for
    a list of properties and methods), except it actually implements
    some of its hook methods to do some work:

    at_basetype_setup - always assigns the default_cmdset to this object type
                    (important!)sets locks so character cannot be picked up
                    and its commands only be called by itself, not anyone else.
                    (to change things, use at_object_creation() instead)
    at_after_move - launches the "look" command
    at_disconnect - stores the current location, so the "unconnected" character
                    object does not need to stay on grid but can be given a
                    None-location while offline.
    at_post_login - retrieves the character's old location and puts it back
                    on the grid with a "charname has connected" message echoed
                    to the room

    """
    def at_object_creation(self):
        """
        Setup character-specific variables.
        """
        super(MyCharacter, self).at_object_creation()
        self.db.r_desc = "%s e' qui." % self.name.capitalize()
        self.db.desc = ""
        self.db.weight = 70
        self.db.height = 180
        self.locks.add("call:false()")
        self.db.equip = {'Testa' : None,
                               'Orecchio destro' : None,
                               'Orecchio sinistro' : None,
                               'Collo' : None,
                               'Spalle' : None,
                               'Busto' : None,
                               'Braccio destro' : None,
                               'Braccio sinistro' : None,
                               'Mano destra' : None,
                               'Mano sinistra' : None,
                               'Dito destro' : None,
                               'Dito sinistro' : None,
                               'Vita' : None,
                               'Gambe' : None,
                               'Piedi' : None }



    def return_appearance(self, pobject):
        """
        This is a convenient hook for a 'look'
        command to call.
        """
        string = super(MyCharacter, self).return_appearance(pobject)
        string += "\n%s" % self.show_equip(pobject)
        return string

    def show_equip(self, pobject):
        """
        Show character's equip.
        """
        string = ""
        equip = dict(self.db.equip) # cut the link with the dbobj
        if not equip:
            return string
        cols = [[],[]]
        equip_view = equip.viewitems()
        for element in equip_view:
            if element[1]:
                if element[1].access(pobject, 'view'):
                    cols[0].append(element[0])
                    cols[1].append(element[1])

        if cols[0]:
            ftable = utils.format_table(cols)
            string += "{wEquipaggiamento:{n"
            for row in ftable:
                string += "\n " + "[%s] - %s" % (row[0].strip(), row[1].capitalize())
        return string

    def announce_move_from(self, destination):
        """
        Called if the move is to be announced. This is
        called while we are still standing in the old
        location.

        destination - the place we are going to.
        """
        if not self.location:
            return
        name = self.name
        #loc_name = ""
        #loc_name = self.location.name
        dest_name = destination.name
        string = "%s va verso %s."
        self.location.msg_contents(string % (name, dest_name), exclude=self)

    def announce_move_to(self, source_location):
        """
        Called after the move if the move was not quiet. At this
        point we are standing in the new location.

        source_location - the place we came from
        """

        name = self.name
        if not source_location and self.location.has_player:
            # This was created from nowhere and added to a player's
            # inventory; it's probably the result of a create command.
            string = "Adesso hai %s in tuo possesso." % name
            self.location.msg(string)
            return

        src_name = "nessuna parte"
        loc_name = self.location.name
        if source_location:
            src_name = source_location.name
        string = "%s arriva da %s."
        self.location.msg_contents(string % (name, src_name), exclude=self)

    def at_disconnect(self):
        """
        We stove away the character when logging off, otherwise the character object will
        remain in the room also after the player logged off ("headless", so to say).
        """
        if self.location: # have to check, in case of multiple connections closing
            self.location.msg_contents("%s ha lasciato il gioco." % self.name, exclude=[self])
            self.db.prelogout_location = self.location
            self.location = None

    def at_post_login(self):
        """
        This recovers the character again after having been "stoved away" at disconnect.
        """
        if self.db.prelogout_location:
            # try to recover
            self.location = self.db.prelogout_location
        if self.location == None:
            # make sure location is never None (home should always exist)
            self.location = self.home
        # save location again to be sure
        self.db.prelogout_location = self.location

        self.location.msg_contents("%s e' entrato nel gioco." % self.name, exclude=[self])
        self.location.at_object_receive(self, self.location)
        # call look
        self.execute_cmd("look")

    def at_object_leave(self, moved_obj, target_location):
        """
        Called just before an object leaves from inside this object

        moved_obj - the object leaving
        target_location - where the object is going.
        """
        # check whether moved_obj is in self.equip, if so, remove it.
        if moved_obj in self.db.equip.values():
            self.db.equip[moved_obj] = None

    def is_alive(self):
        """
        Check if the 
        """
        pass

    def at_death(self, killer):
        """
        Hook for death.
        """
        pass

    def weight(self):
        """
        Returns the weight of the object.
        """
        return self.db.weight
